package com.fadadianqi.logistics.search.web;

import com.fadadianqi.commons.JSONProvider;
import com.fadadianqi.logistics.customer.model.Customer;
import com.fadadianqi.logistics.customer.service.CustomerService;
import com.fadadianqi.logistics.search.CustomerSearcher;
import com.fadadianqi.logistics.search.CustomerSearcher.CustomerVO;
import com.fadadianqi.search.Result;
import com.yeqiangwei.commons.util.Pagination;
import com.yeqiangwei.commons.util.ParamUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Controller("search_customerController")
public class CustomerController {

    private static final Log log = LogFactory.getLog(CustomerController.class);

    @RequestMapping("/a/ajax/search/customer")
    @ResponseBody
    public ResponseEntity<String> ajaxSearch(HttpServletRequest request) {

        String q = ParamUtils.getString(request, "q", null);
        log.info("#ajaxSearch# q: " + q);
        Pagination pagination = new Pagination(request, 30);
        Result result = null;
        try {
            result = customerSearcher.search(q, pagination);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e);
        }

        List<SearchResult> rslist = new ArrayList<CustomerController.SearchResult>();

        if (result != null && result.getItems() != null) {
            List<?> list = result.getItems();
            for (Object o : list) {
                SearchResult rs = new SearchResult();
                CustomerVO c = (CustomerVO) o;
                rs.setTownId(c.getTownId());
                rs.setTownName(c.getTownName());
                rs.setVillageId(c.getVillageId());
                rs.setVillageName(c.getVillageName());
                rs.setCustomerId(c.getId());
                rs.setCustomerName(c.getName());
                rs.setTel(c.getTel());
                rs.setMobile(c.getMobile());
                Customer customer = customerService.getById(c.getId());
                rs.setMemo(customer.getMemo());
                rs.setBalance(customer.getBalance());
                rs.setDebt(customer.getDebt());
                rslist.add(rs);
            }
        }
        String json = JSONProvider.toJSON(rslist);
        log.info(json);
        HttpHeaders responseHeaders = new HttpHeaders();
        responseHeaders.add("Content-Type", "text/html; charset=utf-8");
        //String json = JSONProvider.toJSON(str.toString().split(" "));
        return new ResponseEntity<String>(json,
                responseHeaders, HttpStatus.OK);
    }

	/*@Resource(name="customerService")
	private CustomerService customerService;*/

    @Resource(name = "customerSearcher")
    private CustomerSearcher customerSearcher;

    @Autowired
    private CustomerService customerService;

    public static class SearchResult {

        private String villageId;
        private String villageName;
        private String townId;
        private String townName;
        private String customerId;
        private String customerName;
        private String mobile;
        private String tel;
        private String memo;
        private Double balance;
        private Double debt;

        public Double getDebt() {
            return debt;
        }

        public void setDebt(Double debt) {
            this.debt = debt;
        }

        public Double getBalance() {
            return balance;
        }

        public void setBalance(Double balance) {
            this.balance = balance;
        }

        public String getMemo() {
            return memo;
        }

        public void setMemo(String memo) {
            this.memo = memo;
        }

        public String getMobile() {
            return mobile;
        }

        public void setMobile(String mobile) {
            this.mobile = mobile;
        }

        public String getTel() {
            return tel;
        }

        public void setTel(String tel) {
            this.tel = tel;
        }

        public String getVillageId() {
            return villageId;
        }

        public void setVillageId(String villageId) {
            this.villageId = villageId;
        }

        public String getVillageName() {
            return villageName;
        }

        public void setVillageName(String villageName) {
            this.villageName = villageName;
        }

        public String getTownId() {
            return townId;
        }

        public void setTownId(String townId) {
            this.townId = townId;
        }

        public String getTownName() {
            return townName;
        }

        public void setTownName(String townName) {
            this.townName = townName;
        }

        public String getCustomerId() {
            return customerId;
        }

        public void setCustomerId(String customerId) {
            this.customerId = customerId;
        }

        public String getCustomerName() {
            return customerName;
        }

        public void setCustomerName(String customerName) {
            this.customerName = customerName;
        }
    }
}
